package com.itstyle.seckill.topic.redis.common;

import com.itstyle.seckill.topic.redis.Iservice.Request;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;


/**
 * @Auther: liuwenxiang
 * @Date: 2020/2/23 18:02
 * @Description:
 */
public class RequestProcessorThreadPool {

    private static final int blockingQueueNum = 10;
    private static final int queueDataNum = 100;

    //设置线程池，池大小为阻塞队列的数目
    private ExecutorService threadPool = Executors.newFixedThreadPool(blockingQueueNum);

    private RequestProcessorThreadPool() {
        for (int i = 0; i < blockingQueueNum; i++) {//初始化队列
            ArrayBlockingQueue<Request> queue = new ArrayBlockingQueue<Request>(queueDataNum);//每个队列中放100条数据
            RequestQueue.getInstance().addQueue(queue);
            //这个位置有问题，线程池中的线程只在项目启动的时候执行，执行完了之后，后面不在执行
            threadPool.submit(new RequestProcessorThread(queue));//把每个queue交个线程去处理，线程会处理每个queue中的数据
        }
    }

    public static class Singleton {
        private static RequestProcessorThreadPool instance;

        static {
            instance = new RequestProcessorThreadPool();
        }

        public static RequestProcessorThreadPool getInstance() {
            return instance;
        }
    }

    public static RequestProcessorThreadPool getInstance() {
        return Singleton.getInstance();
    }

    /**
     * 初始化线程池
     */
    public static void init() {
        //通过单例模式创建一个请求池
        getInstance();
    }
}
